package B2;


import com.github.oxo42.stateless4j.StateMachine;
import com.github.oxo42.stateless4j.StateMachineConfig;


public class ControlPanel {

    private StateMachineConfig<State, Event> config;
    private GuardCondition guardCondition = new GuardCondition();
    private StateMachine<State, Event> stateMachine;
    public ControlPanel(){
        init();
    }

    public void init() {
        guardCondition.setIsLock(false);
        guardCondition.setPwd_is_correct(false);
        // 创建状态机配置
        config = new StateMachineConfig<>();

        // 定义状态转换规则
        config.configure(State.Reading)
                .permitIf(Event.toComparing,State.Comparing,guardCondition.hasatry())
                .ignore(Event.toReading)
                .ignore(Event.toLocked)
                .ignore(Event.toSelecting);

        config.configure(State.Comparing)
                .permitIf(Event.toSelecting,State.Selecting,guardCondition.iscorrect(),new Password_correct())
                .permitIf(Event.toLocked,State.Locked,guardCondition.isLocking(),new Locking(this))
                .permitIf(Event.toReading,State.Reading,guardCondition.haschance(),new Password_wrong())
                .ignoreIf(Event.toLocked,guardCondition.isnotLocking())
                .ignoreIf(Event.toSelecting,guardCondition.isnotcorrect())
                .ignore(Event.toComparing);

        config.configure(State.Locked)
                .permitIf(Event.toReading,State.Reading,guardCondition.isnotLocking(),new Lockend())
                .ignoreIf(Event.toReading,guardCondition.isLocking())
                .ignore(Event.toSelecting)
                .ignore(Event.toLocked)
                .ignore(Event.toComparing);

        config.configure(State.Selecting)
                .permit(Event.toReading,State.Reading,new Enter_select_success())
                .ignore(Event.toSelecting)
                .ignore(Event.toLocked)
                .ignore(Event.toComparing);

        // 创建状态机实例
        stateMachine = new StateMachine<>(State.Reading, config);
    }
    public void add_a_character(){
        guardCondition.add_a_character();
    }
    public void add_a_wa_cnt(){
        guardCondition.add_a_wa_cnt();
    }
    public void setPwd_is_correct(boolean now){
        guardCondition.setPwd_is_correct(now);
    }
    public int get_Wa() {
        return guardCondition.get_wa_cnt();
    }
    public void fire(Event event) {
        this.stateMachine.fire(event);
    }
    public void setLock(){
        guardCondition.setIsLock(false);
    }
    public State getCurrentState() {
        return this.stateMachine.getState();
    }
    public void printCurrentState() {
        System.out.println("Current State: " + stateMachine.getState());
    }
}

